package com.retrozelda.nyanimals;

import android.graphics.Color;

public class CProgressBar implements INyanObject
{
	float[] m_MMatrix_Front = new float[16];
	float[] m_MMatrix_Back = new float[16];
	float[] m_MVPMatrix_Front = new float[16];
	float[] m_MVPMatrix_Back = new float[16];
	
	CRectangle m_BackgroundBar;
	CRectangle m_ForegroundBar;
	
	int m_posX;
	int m_posY;
	int m_width;
	int m_height;
	int m_maxValue;
	int m_curValue;
	float m_percent;
	
	
	public CProgressBar()
	{
		m_BackgroundBar = new CRectangle();
		m_ForegroundBar = new CRectangle();
	}
	
	@Override
	public Object clone()
	{
		CProgressBar ret = null;
		try
		{
			ret = (CProgressBar)super.clone();

			ret.m_MMatrix_Front = m_MMatrix_Front.clone();
			ret.m_MMatrix_Back = m_MMatrix_Back.clone();
			ret.m_MVPMatrix_Front = m_MVPMatrix_Front.clone();
			ret.m_MVPMatrix_Back = m_MVPMatrix_Back.clone();
			
			ret.m_BackgroundBar = (CRectangle)m_BackgroundBar.clone();
			ret.m_ForegroundBar = (CRectangle)m_ForegroundBar.clone();
			
			ret.m_posX = m_posX;
			ret.m_posY = m_posY;
			ret.m_width = m_width;
			ret.m_height = m_height;
			ret.m_maxValue = m_maxValue;
			ret.m_curValue = m_curValue;
			ret.m_percent = m_percent;			
			
		}
		catch (CloneNotSupportedException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return ret;
	}
	
	public void SetCurrentValue(int val)
	{
		m_curValue = val;
	}

	public void SetMaxValue(int val)
	{
		m_maxValue = val;
	}

	public int GetPercent()
	{
		return (int)((m_percent * 100.0f) + 1.0f);
	}

	public int[] getPosition()
	{
		int[] pos = new int[2];
		pos[0] = m_posX;
		pos[1] = m_posY;
		
		return pos;
	}
	
	// NOTE: posX and posY are 0 -> 1 to be on screen
	public void Init(NyanimalGLSurfaceView game, int frontColor, int backColor, int posX, int posY, int width, int height, int maxValue, int curValue)
	{

		m_width = width;
		m_height = height;
		m_posX = posX;
		m_posY = posY;
		m_maxValue = maxValue;
		m_curValue = curValue;
		
		int R, G, B, A;
		A = (frontColor >> 24) & 0xFF;
		R = (frontColor >> 16) & 0xFF;
		G = (frontColor >> 8) & 0xFF;
		B = (frontColor) & 0xFF;
		m_ForegroundBar.Init(game, R, G, B, A);
		m_ForegroundBar.SetSize(m_width, m_height);
		m_ForegroundBar.SetPosition(m_posX + (m_width), m_posY + (m_height / 2));

		A = (backColor >> 24) & 0xFF;
		R = (backColor >> 16) & 0xFF;
		G = (backColor >> 8) & 0xFF;
		B = (backColor) & 0xFF;
		m_BackgroundBar.Init(game, R, G, B, A);
		m_BackgroundBar.SetSize(m_width, m_height);
		m_BackgroundBar.SetPosition(m_posX + (m_width), m_posY + (m_height / 2));
	}
	
	@Override
	public void Init(NyanimalGLSurfaceView game)
	{
		Init(game, Color.GREEN, Color.RED, 10, 32, 128, 32, 1000, 0);
	}

	@Override
	public void Update(float deltaTime)
	{		
		if(m_curValue > m_maxValue)
		{
			m_curValue = m_maxValue;
		}
		m_percent = (float)m_curValue / (float)m_maxValue;
	}

	@Override
	public void Draw(CArgumentGroup arg)
	{		
		// set the position
		m_ForegroundBar.SetPosition(m_posX  + (int)(m_width * m_percent), m_posY + (m_height / 2));
		
		m_ForegroundBar.SetSize((int)(m_width * m_percent), m_height);
		////////////////////////////////////////
		
		m_BackgroundBar.Draw(arg);
		m_ForegroundBar.Draw(arg);
	}
}
